package com.github_Dididipapa.shortlink.project.toolkit;

import java.security.SecureRandom;

/**
 * @description: 随机字符串生成工具类
 * @author: dididi_papa
 * @date: 2025/9/11 下午2:19
 * @param:
 * @return:
 **/
public class RandomCodeGenerator {

    private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    private static final int CODE_LENGTH = 6;
    private static final SecureRandom random = new SecureRandom();

    /**
     * 生成6位随机字母数字字符串
     * @return 6位随机字符串
     */
    public static String generateRandomCode() {
        StringBuilder code = new StringBuilder(CODE_LENGTH);
        for (int i = 0; i < CODE_LENGTH; i++) {
            int index = random.nextInt(CHARACTERS.length());
            code.append(CHARACTERS.charAt(index));
        }
        return code.toString();
    }

    /**
     * 生成指定长度的随机字母数字字符串
     * @param length 字符串长度
     * @return 指定长度的随机字符串
     */
    public static String generateRandomCode(int length) {
        StringBuilder code = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(CHARACTERS.length());
            code.append(CHARACTERS.charAt(index));
        }
        return code.toString();
    }
}
